package com.hncg.dao;

import com.hncg.entity.User;
import com.hncg.util.DBUtil;
import jdk.internal.org.jline.terminal.impl.ExecPty;
import org.graalvm.compiler.phases.common.util.EconomicSetNodeEventListener;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    //获得连接
    Connection conn = DBUtil.getConnection();
    PreparedStatement ps = null;
    ResultSet rs=null;
    //查询所有用户
    public List<User> getAllUser() {
        List<User> list = new ArrayList<>();

        //编写sql语句
        String sql = "select * from t_user";
        try {

            //预编译sql语句
            ps = conn.prepareStatement(sql);
            //执行查询并返回结果集
             rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setAge(rs.getInt("age"));
                user.setHiredate(rs.getString("hiredate"));

                list.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 用户登录
     *
     * @param name
     * @param pwd
     * @return
     */
    public boolean userLogin(String name, String pwd) {
        boolean flag = false;
        String sql = "select * from t_user where name = ? and pwd = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
             rs = ps.executeQuery();
            if (rs.next()) {
                //如果结果集下一行有数据
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 注册
     * @param user
     * @return
     */
    public boolean userReg(User user) {
        boolean flag = false;
        //错误:如果不将主键设置为int,并设置为自增,就必须手动添加主键,否则sql语句运行不成功
        String sql = "insert into t_user(name,pwd,age) values (?,?,?)";
        try {

            //预编译sql语句
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getName());
            ps.setString(2,user.getPwd());
            ps.setInt(3,user.getAge());
//            ps.setString(4,user.getHiredate());
            //增删改使用executUpdate,代表受影响行数
            int result =  ps.executeUpdate();
            if (result>0){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 根据id获得用户信息
     * @param id
     * @return
     */
    public User getUserById(int id){
        User user = null;
        String sql = "select * from t_user where id=?";
        try{
            ps=conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPwd(rs.getString("pwd"));
                user.setAge(rs.getInt("age"));
                user.setHiredate(rs.getString("hiredate"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    public boolean updateUser(User user){
        boolean flag = false;
        String sql = "update t_user set name = ?,pwd=?,age=? where id = ?";
        try{
            ps=conn.prepareStatement(sql);
            ps.setString(1,user.getName());
            ps.setString(2,user.getPwd());
            ps.setInt(3,user.getAge());
            ps.setInt(4,user.getId());
            int result = ps.executeUpdate();
            if (result>0){
                flag = true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 自己写的
     * @param id
     * @return
     */

    public  boolean deleteUser(int id){
        boolean flag = false;
        String sql = "delete * from t_user where id=?";
        try{
            ps= conn.prepareStatement(sql);
            int result = ps.executeUpdate();
            if (result>0){
                flag = true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }
    /**
     * 老师版本
     */
    public boolean delUser(int id){
        String sql = "delete from t_user where id = ?";
        try{

            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            int result = ps.executeUpdate();
            if (result>0){
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }
}